﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.Entity;

using Happy.Domain.EntityFramework;
using Happy.Examples.OrderManagement.Domain.Orders;

namespace Happy.Examples.OrderManagement.Domain.EntityFramework
{
    internal sealed class OrderManagementUnitOfWork : UnitOfWork
    {
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);

            modelBuilder
                .ComplexType<CustomerInfo>()
                .Property(x => x.Name).HasColumnName("CustomerName");
            modelBuilder
                .ComplexType<CustomerInfo>()
                .Property(x => x.Address).HasColumnName("CustomerAddress");
            modelBuilder
                .ComplexType<CustomerInfo>()
                .Property(x => x.Tel).HasColumnName("CustomerTel");

            modelBuilder
                .Entity<OrderItem>()
                .HasKey(x => new { x.Id, x.OrderId });

            modelBuilder
                .Entity<Order>()
                .HasKey(x => x.Id);
            modelBuilder
                .Entity<Order>()
                .Property(x => x.OptimisticKey).IsRowVersion().IsConcurrencyToken();
            modelBuilder
                .Entity<Order>()
                .HasMany(x => x.OrderItemCollection).WithRequired().HasForeignKey(x => x.OrderId).WillCascadeOnDelete();

        }
    }
}
